package com.tutor.lzm.function.teacher.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tutor.lzm.R;

/**
 * Created by zzgpc on 2017-10-6.
 * 选择授课时间
 */
public class GridSelectTimeAdapter extends BaseAdapter {
    private Context context;
    ViewHolder viewHolder;

    public boolean isClick() {
        return isClick;
    }

    public void setClick(boolean click) {
        isClick = click;
    }

    private boolean isClick = true;

    public boolean[] getSelectPosFlag() {
        return selectPosFlag;
    }

    public void setSelectPosFlag(int position) {
        selectPosFlag[position] = true;
    }

    /**
     * 是否选中标志位
     */
    private boolean[] selectPosFlag = new boolean[24];

    public GridSelectTimeAdapter(Context context) {
        this.context = context;
        for (int i = 0; i < 24; i++) {
            selectPosFlag[i] = false;
        }
    }

    @Override
    public int getCount() {
        return 24;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_select_time, null);
            viewHolder = new ViewHolder();
            viewHolder.rlItem = (RelativeLayout) convertView.findViewById(R.id.rl_item);
            viewHolder.tvShow = (TextView) convertView.findViewById(R.id.tv_show);
            viewHolder.imgSelect = (ImageView) convertView.findViewById(R.id.img_select);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final int pos = position;
        if (pos == 0) {
            viewHolder.tvShow.setText("上\n午");
        } else if (pos == 8) {
            viewHolder.tvShow.setText("下\n午");
        } else if (pos == 16) {
            viewHolder.tvShow.setText("晚\n上");
        } else {
            viewHolder.tvShow.setText("");
        }
        if (pos % 2 == 1) {
            viewHolder.rlItem.setBackgroundColor(context.getResources().getColor(R.color.blue_select));
        }

        if (pos == 0 || pos == 8 || pos == 16) {
            viewHolder.imgSelect.setBackground(null);
        } else {
            if (selectPosFlag[pos]) {
//                viewHolder.imgSelect.setBackground(null);
                viewHolder.imgSelect.setVisibility(View.VISIBLE);
                Picasso.with(context)
                        .load(R.mipmap.gouxuan)
                        .into(viewHolder.imgSelect);
            } else {
                viewHolder.imgSelect.setVisibility(View.GONE);
            }
        }

        viewHolder.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClick || pos == 0 || pos == 8 || pos == 16) {
                    return;
                }
                if (selectPosFlag[pos]) {
                    selectPosFlag[pos] = false;
                } else {
                    selectPosFlag[pos] = true;
                }
                notifyDataSetChanged();
            }
        });
        return convertView;
    }


    class ViewHolder {
        RelativeLayout rlItem;
        TextView tvShow;
        ImageView imgSelect;
    }
}
